<template>
 <div>
  <p v-ml="val"></p>
  <input type="text"  v-focus>
  <ul>
    <li v-for="item in  list" :key="item.id">
      <img style="width:200px" :src="item.img" alt="">
      {{ item.title }}
    </li>
  </ul>
  <div v-loading="list.length==0"></div>
  <img v-errorImg="'http://ajax-api.itheima.net/images/4.webp'" style="width:200px" src="http://ajax-api.itheima.net/images/0.webp" alt="">
 </div>
</template>
<script>
import errorImg  from  './assets/logo.png'
import axios  from  'axios'
export default{
    data () {
        return {
          val:'自定义',
          list:[]
        }
    },
    directives:{
      ml:{
        inserted(el,binding){
          el.innerHTML=binding.value
        }
      },
      loading:{
        inserted(el,binding){
          el.classList.add('active')
          el.innerHTML='正在加载中,请稍后'
        },
        update(el,binding){
         setTimeout(()=>{
          if(!binding.value){
            el.style.display='none'
          }
         },2000)
        }
      },
      errorImg:{
        inserted(el,binding){
          el.onerror=function(){
            console.log(binding.value);
            el.src=binding.value
          }
        }
      }
    },
    created () {
      this.getNews()
    },
    computed:{
    },
    methods:{
      async getNews(){
       const  res=await this.$axios.get('http://hmajax.itheima.net/api/news')
       console.log(res);
       this.list=res.data.data
      }
    }
}
</script>
<style lang='less'  scoped>
    .active{
  width: 100vw;
  height: 100vh;
  background:rgba(158, 156, 156, 0.7);
  z-index:999;
  position: fixed;
  top: 0;
  left:0;
  text-align: center;
  line-height: 100vh;
  font-size: 30px;
  color: red;
 }
</style>
